package xyz.app.queries;

import jakarta.persistence.EntityManager;
import jakarta.persistence.Query;
import lombok.NonNull;
import xyz.app.dependencies.HasDomainMembers;
import xyz.app.AbstractDomain;

import java.util.Optional;

public interface EntityGraphCreator<T extends AbstractDomain> {

    String GRAPH_HINT_NAME = "jakarta.persistence.fetchgraph";

    Optional<HasDomainMembers<?>> hasMembers();

    @NonNull Class<T> domainClass();

    default void createGraph(@NonNull EntityManager manager, @NonNull Query query) {
        hasMembers().ifPresent(hasMembers -> {
            var graph = manager.createEntityGraph(domainClass());
            hasMembers.configureGraph(graph);
            query.setHint(GRAPH_HINT_NAME, graph);
        });
    }
}